# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import requests
from itertools import product
from common.helper import Helper
from common.config import *
from urllib.parse import urlencode


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "机动车驾驶人员意外伤害保险"

    # 生成产品数据
    def genProductInfo(self):
        plan = {
            "planA": {
                "days": ["365d"],
                "people_count": ["1"],
                "plan": [
                    {"service_death": "50000"},
                    {"private_death": "50000"},
                    {"taxi_death": "50000"},
                    {"travel_death": "50000"},
                    {"passenger_death": "50000"},
                    {"goods_death": "50000"},

                ],
                "accident_medical": ["5000"]

            },
            "planB": {
                "days": ["365d"],
                "people_count": ["1"],
                "plan": [
                    {"service_death": "100000"},
                    {"private_death": "100000"},
                    {"taxi_death": "100000"},
                    {"travel_death": "100000"},
                    {"passenger_death": "100000"},
                    {"goods_death": "100000"},

                ],
                "accident_medical": ["20000"]

            },

        }
        data = {}
        for k in plan:
            for v in product(plan[k]["days"], plan[k]["people_count"], plan[k]["plan"], plan[k]["accident_medical"]):
                data["days"] = v[0]
                data["people_count"] = v[1]
                if "service_death" in v[2]:
                    data["service_death"] = "50000"
                    data["private_death"] = ""
                    data["taxi_death"] = ""
                    data["travel_death"] = ""
                    data["passenger_death"] = ""
                    data["goods_death"] = ""
                elif "private_death" in v[2]:
                    data["service_death"] = ""
                    data["private_death"] = "50000"
                    data["taxi_death"] = ""
                    data["travel_death"] = ""
                    data["passenger_death"] = ""
                    data["goods_death"] = ""
                elif "taxi_death" in v[2]:
                    data["service_death"] = ""
                    data["private_death"] = ""
                    data["taxi_death"] = "50000"
                    data["travel_death"] = ""
                    data["passenger_death"] = ""
                    data["goods_death"] = ""
                elif "travel_death" in v[2]:
                    data["service_death"] = ""
                    data["private_death"] = ""
                    data["taxi_death"] = ""
                    data["travel_death"] = "50000"
                    data["passenger_death"] = ""
                    data["goods_death"] = ""
                elif "passenger_death" in v[2]:
                    data["service_death"] = ""
                    data["private_death"] = ""
                    data["taxi_death"] = ""
                    data["travel_death"] = ""
                    data["passenger_death"] = "50000"
                    data["goods_death"] = ""
                else:
                    data["service_death"] = ""
                    data["private_death"] = ""
                    data["taxi_death"] = ""
                    data["travel_death"] = ""
                    data["passenger_death"] = ""
                    data["goods_death"] = "50000"
                data["accident_medical"] = v[3]
                yield data

    # 执行爬虫
    def requestJson(self):

        for v in self.genProductInfo():
            params = '''
            insuranceid_24184_保险金额: 100000
            insuranceid_24185_保险金额: 10000
            suiteId: 5923
            period: 365
            amount: 
            carDType: 1
            personCount: 1
            backInsuranceCount: 
            youngAccident: NvhPersonalAccident
            '''
            data = Helper.perfectJson(params)
            if v["accident_medical"] == "5000":
                if v["service_death"] != "":
                    data["carDType"] = "1"
                else:
                    pass
                if v["private_death"] != "":
                    data["carDType"] = "2"
                else:
                    pass
                if v["taxi_death"] != "":
                    data["carDType"] = "3"
                else:
                    pass
                if v["travel_death"] != "":
                    data["carDType"] = "4"
                else:
                    pass
                if v["passenger_death"] != "":
                    data["carDType"] = "5"
                else:
                    pass
                if v["goods_death"] != "":
                    data["carDType"] = "6"
                else:
                    pass
                data["insuranceid_24194_保险金额"] = "50000"
                data["insuranceid_24195_保险金额"] = "5000"
            else:
                if v["service_death"] != "":
                    data["carDType"] = "1"
                else:
                    pass
                if v["private_death"] != "":
                    data["carDType"] = "2"
                else:
                    pass
                if v["taxi_death"] != "":
                    data["carDType"] = "3"
                else:
                    pass
                if v["travel_death"] != "":
                    data["carDType"] = "4"
                else:
                    pass
                if v["passenger_death"] != "":
                    data["carDType"] = "5"
                else:
                    pass
                if v["goods_death"] != "":
                    data["carDType"] = "6"
                else:
                    pass
                data["insuranceid_24184_保险金额"] = "100000"
                data["insuranceid_24185_保险金额"] = "10000"

            payload = urlencode(data).replace("+", "")
            r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)
            if r.status_code == 200 and "totalPrice" in r.text:
                price = Helper.decimalParse(r.json()["totalPrice"])
                data = {
                    "name": self.name,
                    "company": "北部湾财产保险",
                    "price": price
                }
                data.update(v)
                Helper.mergeTable(self.name, self.mysql, data)
